package cn.edu.cug.cs.gtl.docsrv.auth.dao.imp;

import cn.edu.cug.cs.gtl.docsrv.auth.dao.GroupDao;
import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthGroup;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthGroupMapper;
import org.springframework.stereotype.Component;

@Component
public class GroupDaoImpl implements GroupDao {
    @Override
    public boolean insertGroup(AuthGroup authGroup) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthGroupMapper mapper = s.getMapper(AuthGroupMapper.class);
            mapper.insert(authGroup);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean updateGroup(AuthGroup authGroup) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthGroupMapper mapper = s.getMapper(AuthGroupMapper.class);
            mapper.updateByGroupName(authGroup);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteGroup(String authGroupName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthGroupMapper mapper = s.getMapper(AuthGroupMapper.class);
            mapper.deleteByGroupName(authGroupName);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public AuthGroup getGroup(String authGroupName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthGroupMapper mapper = s.getMapper(AuthGroupMapper.class);
            AuthGroup authGroup = mapper.selectByGroupName(authGroupName);
            s.close();
            return authGroup;
        }
        return null;
    }
}
